﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Supervise_TraHospitalCompanyGoods : System.Web.UI.Page
{
    public string _strUserType = "", _AccountId_SC = "", _strEndDate = "", _strStartDate = "", _AreaId = "", _AccountId_PS = "", _strAccountId_YY = "", _AccountId_TB;
    string _strAccountid = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        string strSql, _SortId = "", _BuyPlanId, _strHospitalName;
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumSupervise);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumSuperviseChildCheck);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["deal"] != null && Request.QueryString["deal"] != "")
                {
                    switch (Request.QueryString["deal"].ToString())
                    {
                        case "未确认送货":
                            dealDrop.SelectedIndex = 0;
                            break;
                        case "送货中":
                            dealDrop.SelectedIndex = 1;
                            break;
                        case "已入库":
                            dealDrop.SelectedIndex = 2;
                            break;
                        case "无法配送":
                            dealDrop.SelectedIndex = 3;
                            break;
                        case "到期未送":
                            dealDrop.SelectedIndex = 4;
                            break;

                    }

                }
            }
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                _AreaId = SysFun.IsSQL(_Account.AreaId);
                _strUserType = SysFun.IsSQL(_Account.Type);
                _strAccountId_YY = SysFun.IsSQL(_Account.AccountId);
                _strAccountid = SysFun.IsSQL(_Account.AccountId);
                _strHospitalName = SysFun.IsSQL(_Account.AccountName);

            }

            _AccountId_PS = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["AccountId_PS"]));
            _AccountId_SC = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["AccountId_SC"]));
            _AccountId_TB = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["AccountId_TB"]));
            if (SysFun.ToTrim(Request.QueryString["AreaId"]) != "")
            {
                _AreaId = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["AreaId"]));
            }
            if (_strUserType != "3")
            {
                _strAccountId_YY = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["AccountId_YY"]));
            }
            if (!IsPostBack)
            {
                lstQualityLevals.Items.Add("全部");
                lstQualityLevals.Items.Add("普通GMP药品");
                lstQualityLevals.Items.Add("专利药品");
                lstQualityLevals.Items.Add("1999年以来获得国家科学技术奖的药品");
                lstQualityLevals.Items.Add("国家一类新药");
                lstQualityLevals.Items.Add("原研制药品");
                lstQualityLevals.Items.Add("单独定价药品");
                lstQualityLevals.Items.Add("优质优价中成药");
                lstQualityLevals.Items.Add("全国医药工业百强企业的药品");
                lstQualityLevals.Items.Add("中药保护品种");
                lstQualityLevals.Items.Add("欧美认证药品");
                //<!--初始化统计时间

                int Year, Month, Day, DMonth, Year1;
                Year = DateTime.Now.Year;
                Year1 = Year;
                DMonth = DateTime.Now.Month;
                Month = DMonth - 1;
                if (Month == 0)
                {
                    Year1 -= 1;
                    Month = 12;
                }

                Day = DateTime.Now.Day;
                txtStartDate.Text = Year1.ToString() + "-" + Month.ToString() + "-01";
                txtEndDate.Text = Year.ToString() + "-" + DMonth.ToString() + "-01";

                _strEndDate = SysFun.IsSQL(Request.QueryString["EndDate"]);
                _strStartDate = SysFun.IsSQL(Request.QueryString["StartDate"]);
                if (_strEndDate != "")
                {
                    txtEndDate.Text = _strEndDate;
                }
                if (_strStartDate != "")
                {
                    txtStartDate.Text = _strStartDate;
                }
                //初始化统计时间-->


                Db DbProject = new Db();
                System.Data.IDataReader dr;


                //<!--初始化通用名

                txtProductName.Text = SysFun.IsSQL(Request.QueryString["ProductName"]);
                //初始化通用名-->

                //<!--初始化流水号
                txtGoodsId.Text = SysFun.IsSQL(Request.QueryString["GoodsId"]);
                //初始化流水号-->

                //<!--初始化配送企业

                if (_strUserType == "2")
                {
                    _AreaId = "530000";
                    if (_Account.Sort == "0")
                    {
                        strSql = "select AccountId,CompanyName,UserCode from company  where Accountid= " + _strAccountid; //此时是生产企业

                        dr = DbProject.DataReader(strSql);
                        while (dr.Read())
                        {
                            txtCompanyNameSC.Text = dr["companyname"].ToString();
                            txtUserCodeSC.Text = dr["UserCode"].ToString();
                            txtCompanyNameSC.ReadOnly = true;
                            txtUserCodeSC.ReadOnly = true;
                        }
                        dr.Close();
                        dr.Dispose();
                    }
                    else
                    {
                        txtCompanyNamePS.Text = _Account.DWName;
                        txtUserCodePS.Text = _Account.UserCode;
                        txtCompanyNamePS.ReadOnly = true;
                        txtUserCodePS.ReadOnly = true;
                        _AccountId_PS = SysFun.ToTrim(_Account.AccountId);
                    }
                }
                else if (_AccountId_PS != "")
                {
                    strSql = "select AccountId,CompanyName,UserCode from company where Accountid= " + _AccountId_PS;
                    dr = DbProject.DataReader(strSql);
                    while (dr.Read())
                    {
                        txtCompanyNamePS.Text = dr["companyname"].ToString();
                        txtUserCodePS.Text = dr["UserCode"].ToString();
                        txtCompanyNamePS.ReadOnly = true;
                        txtUserCodePS.ReadOnly = true;
                    }
                    dr.Close();
                    dr.Dispose();
                }
                //配送企业-->

                //<!--初始化生产企业

                if (_AccountId_SC != "")
                {
                    strSql = "select AccountId,CompanyName,UserCode from company  where Accountid= " + _AccountId_SC;
                    dr = DbProject.DataReader(strSql);
                    while (dr.Read())
                    {
                        txtCompanyNameSC.Text = dr["companyname"].ToString();
                        txtUserCodeSC.Text = dr["UserCode"].ToString();
                        txtCompanyNameSC.ReadOnly = true;
                        txtUserCodeSC.ReadOnly = true;
                    }
                    dr.Close();
                    dr.Dispose();
                }
                //初始化生产企业-->

                //<!--初始化中标企业

                if (_AccountId_TB != "")
                {
                    strSql = "select AccountId,CompanyName,UserCode from company  where Accountid= " + _AccountId_TB;
                    dr = DbProject.DataReader(strSql);
                    while (dr.Read())
                    {
                        txtCompanyNameZB.Text = dr["companyname"].ToString();
                        txtUserCodeZB.Text = dr["UserCode"].ToString();
                        txtCompanyNameZB.ReadOnly = true;
                        txtUserCodeZB.ReadOnly = true;
                    }
                    dr.Close();
                    dr.Dispose();
                }
                //初始化中标企业-->

                //<!--初始化采购单
                _BuyPlanId = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["BuyPlanId"]));
                if (_BuyPlanId != "")
                {
                    strSql = "select BuyPlanId,PlanName from PurchasePlan  where BuyPlanId= " + _BuyPlanId;
                    dr = DbProject.DataReader(strSql);
                    while (dr.Read())
                    {
                        labPurchasePlanName.Text = dr["PlanName"].ToString();
                        labPurchasePlanName.Visible = true;
                        labBuyPlanId.Text = dr["BuyPlanId"].ToString();
                        Label2.Visible = true;
                    }
                    dr.Close();
                    dr.Dispose();
                }
                else
                {
                    labPurchasePlanName.Visible = false;
                    Label2.Visible = false;
                }
                labBuyPlanId.Visible = false;

                //初始化采购单-->

                //<!--初始化医院

                if (_strUserType != "3")
                {
                    if (_strAccountId_YY != "")
                    {
                        strSql = "select AccountId,HospitalName,UserCode from Hospital  where Accountid= " + _strAccountId_YY;
                        dr = DbProject.DataReader(strSql);
                        while (dr.Read())
                        {
                            txtHospitalName.Text = dr["HospitalName"].ToString();
                            txtHosptialUserCode.Text = dr["UserCode"].ToString();
                            txtHospitalName.ReadOnly = true;
                            txtHosptialUserCode.ReadOnly = true;
                        }
                        dr.Close();
                        dr.Dispose();
                    }
                }
                else
                {
                    txtHospitalName.Text = _Account.DWName;
                    txtHospitalName.Enabled = false;
                    txtHosptialUserCode.Text = _Account.UserCode;
                    txtHosptialUserCode.Enabled = false;
                }
                //初始化医院-->

                int i = 0;
                //<!--初始化大类

                i = 0;
                strSql = "select sortid,sortname from product_sort ";
                dr = DbProject.DataReader(strSql);
                while (dr.Read())
                {
                    lstSort.Items.Add(dr["sortname"].ToString());
                    lstSort.Items[i++].Value = dr["sortid"].ToString();
                }
                lstSort.DataBind();
                dr.Close();
                dr.Dispose();
                _SortId = SysFun.IsSQL(Request.QueryString["SortId"]);
                if (_SortId != "")
                {
                    lstSort.SelectedValue = _SortId;
                }
                //初始化大类-->
                DbProject.Close();


            }

            showGridView();

        }
    }

    protected void Button5_Click(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 查询总共有多少页
    /// </summary>
    /// <returns></returns>
    /// <summary>
    /// 将数据绑定到GridView1
    /// </summary>
    /// <param name="PageIndex">转向第几页</param>
    public void showGridView()
    {

        string strConditions,
            _SelectField = @"case GoodsState when 0 then '原中标'  when 1 then '自行采购'  when 2 then '22种降价'  when 0 then '血液制品'  when 0 then '重点监控' end as GoodsState,SupplyGoodsId,GoodsId,productname,GoodsName,medicinemodel,outlookc,factor,PackUnit,companyName_SC,
                            CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS  QualityLevels,convert(numeric(20,2),TradePrice) as TradePrice,convert(numeric(20,2),BidPrice) as BidPrice,sum(inflownum) as RKSL ,   convert(numeric(20,2),sum(inflownum*TradePrice)) as DHJE,case DealStatus  when 0 then '未确认送货' when 1 then '送货中' when 2 then '已入库' when 4 then '无法配送' when 5 then '到期未送' end as DealStatus";
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "tra_GoodsId";                                               //要查询的表明
        dbPage.SelectFieldGroup = "GoodsState,SupplyGoodsId,GoodsId,productname,GoodsName,medicinemodel,outlookc,factor,PackUnit,companyName_SC,QualityLevels,TradePrice,BidPrice,DealStatus";
        dbPage.Key = "SupplyGoodsId";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = _SelectField;//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   

        if (Request.Form["subToExcel"] == "导出到Excel")
        {
            dbPage.PageSize = 100000;
        }
        else
        {
            dbPage.PageSize = 30;                                                    //没有显示记录条数   
        }

        dbPage.OrderBy = "DHJE,SupplyGoodsId";                  //排序字段
        dbPage.OrderByIsDesc = true;
        dbPage.DGJE = "convert(numeric(20,2),sum(GoodsNum*TradePrice)) as DGJE";
        dbPage.DHJE = "convert(numeric(20,2),sum(inflownum*TradePrice)) as DHJE";
        System.Data.IDataReader dr = dbPage.ExecuteReaderGroup(60);
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键

        if (Request.Form["subToExcel"] == "导出到Excel")
        {
            SysFun.WriteToExcel(GridView1, this.Page, "按交易药品查询.xls");
        }

    }
    protected void ToEcxel()//导出
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        // 添加头信息，为"文件下载/另存为"对话框指定默认文件名 
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("按医院查询.xls"));
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentEncoding = System.Text.Encoding.UTF7;  //解决将gridview导出成excel时出现乱码  
        Response.ContentType = "application/ms-excel";
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.GridView1.RenderControl(oHtmlTextWriter);

        // 把文件流发送到客户端 
        Response.Write(oStringWriter.ToString());
        // 停止页面的执行


        Response.End();


    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //OverRide　为了使导出成Excel可行！

    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {

        string _strTemp = " DealStatus>=0 and DealStatus<>3 and", _strProductName, _strMedicinemodel, _strOutlookc, _strGoodsName, _QualityLevals, _GoodsId,
            _strCompanyNameSC, _UserCodeSC, _strCompanyNameZB, _UserCodeZB, _strCompanyNamePS, _UserCodePS, _strHospitalName,
            _strHosptialUserCode, _strSortId = "", _strBuyPlanId;
        //通用名

        _strProductName = txtProductName.Text.ToString();
        if (_strProductName != "")
        {
            _strTemp += " ( ProductName like '%" + _strProductName + "%' or Product_SpellingCode like '%" + _strProductName + "%' ) and";
        }

        //剂型
        _strMedicinemodel = txtMedicinemodel.Text.ToString();
        if (_strMedicinemodel != "")
        {
            _strTemp += " medicinemodel like '%" + _strMedicinemodel + "%' and";
        }

        //规格
        _strOutlookc = txtOutlook.Text.ToString();
        if (_strOutlookc != "")
        {
            _strTemp += " outlookc like '%" + _strOutlookc + "%' and";
        }

        //商品名

        _strGoodsName = txtGoodsName.Text.ToString();
        if (_strGoodsName != "")
        {
            _strTemp += " goodsname like '%" + _strGoodsName + "%' and";
        }
        if (dealDrop.SelectedValue != "")
        {
            if (dealDrop.SelectedValue == "0")
            {
                _strTemp += " DealStatus=" + dealDrop.SelectedValue + " and buystatus=2 and";
            }
            else
            {
                _strTemp += " DealStatus=" + dealDrop.SelectedValue + " and";
            }
        }

        //质量层次
        _QualityLevals = lstQualityLevals.SelectedValue;
        switch (_QualityLevals)
        {
            case "全部":
                break;
            case "普通GMP药品":
                _strTemp += " qualitylevels=0 and";
                break;
            case "专利药品":
                _strTemp += " qualitylevels=1 and";
                break;
            case "1999年以来获得国家科学技术奖的药品":
                _strTemp += " qualitylevels=2 and";
                break;
            case "国家一类新药":
                _strTemp += " qualitylevels=3 and";
                break;
            case "原研制药品":
                _strTemp += " qualitylevels=4 and";
                break;
            case "单独定价药品":
                _strTemp += " qualitylevels=5 and";
                break;
            case "优质优价中成药":
                _strTemp += " qualitylevels=6 and";
                break;
            case "全国医药工业百强企业的药品":
                _strTemp += " qualitylevels=7 and";
                break;
            case "中药保护品种":
                _strTemp += " qualitylevels=8 and";
                break;
            case "欧美认证药品":
                _strTemp += " qualitylevels=9 and";
                break;
        }

        //药品流水号

        _GoodsId = txtGoodsId.Text.ToString();
        if (_GoodsId != "")
        {
            _strTemp += " GoodsId =" + _GoodsId + " and";
        }
        //生产企业名称
        _strCompanyNameSC = txtCompanyNameSC.Text.ToString();
        if (_strCompanyNameSC != "")
        {
            _strTemp += " CompanyName_SC like '%" + _strCompanyNameSC + "%' and";
        }

        //生产企业编码
        _UserCodeSC = txtUserCodeSC.Text.ToString();
        if (_UserCodeSC != "")
        {
            _strTemp += " UserCode_SC like '%" + _UserCodeSC + "%' and";
        }


        //中标企业名称
        _strCompanyNameZB = txtCompanyNameZB.Text.ToString();
        if (_strCompanyNameZB != "")
        {
            _strTemp += " CompanyName_TB like '%" + _strCompanyNameZB + "%' and";
        }

        //中标企业编码
        _UserCodeZB = txtUserCodeZB.Text.ToString();
        if (_UserCodeZB != "")
        {
            _strTemp += " UserCode_TB like '%" + _UserCodeZB + "%' and";
        }

        //配送企业名称

        _strCompanyNamePS = txtCompanyNamePS.Text.ToString();
        if (_strCompanyNamePS != "")
        {
            _strTemp += " CompanyName_PS like '%" + _strCompanyNamePS + "%' and";
        }

        //配送企业编码

        _UserCodePS = txtUserCodePS.Text.ToString();
        if (_UserCodePS != "")
        {
            _strTemp += " UserCode_PS like '%" + _UserCodePS + "%' and";
        }

        //医院名称
        _strHospitalName = txtHospitalName.Text.ToString();
        if (_strHospitalName != "")
        {
            _strTemp += " HospitalName like '%" + _strHospitalName + "%' and";
        }

        //医院编码
        _strHosptialUserCode = txtHosptialUserCode.Text.ToString();
        if (_strHosptialUserCode != "")
        {
            _strTemp += " UserCode_YY like '%" + _strHosptialUserCode + "%' and";
        }

        //要统计的时间范围
        _strEndDate = txtEndDate.Text.ToString();
        _strStartDate = txtStartDate.Text.ToString();
        if (_strEndDate != "" && _strStartDate != "")
        {
            _strTemp += " isnull(hospitalinflowdate,'3000-12-30') >='" + _strStartDate + "' and isnull(hospitalinflowdate,'1900-01-01') <='" + _strEndDate + "' and";
        }

        //药品类别
        _strSortId = lstSort.SelectedValue;
        if (_strSortId != "0")
        {
            _strTemp += " ( SortId=" + _strSortId + " or FrontSortId=" + _strSortId + " ) and";
        }
        //采购单 labBuyPlanId
        _strBuyPlanId = labBuyPlanId.Text.ToString();
        if (_strBuyPlanId != "")
        {
            _strTemp += "  BuyPlanId=" + _strBuyPlanId + "  and";
        }
        if (_strTemp != "")
        {
            _strTemp = "  " + _strTemp.Substring(0, _strTemp.Length - 3);
        }

        return _strTemp;

    }
}
